﻿@page "/tree-grid/inline-editing"

@using Syncfusion.Blazor.TreeGrid
@using Syncfusion.Blazor.DropDowns
@using Syncfusion.Blazor.Grids
@*Hidden:Lines*@
@using ej2_blazor_wrapdata
@inherits SampleBaseComponent;
@*End:Hidden*@


<SampleDescription>
    <p>This sample demonstrates CRUD operations in Tree Grid. You can perform CRUD operations as follows,</p>
    <ul>
        <li><code>Add </code> - To add new record, click Add toolbar button.</li>
        <li><code>Edit</code> - To edit record, double click a row or click toolbar Edit button after selecting a row.</li>
        <li><code>Delete</code> - To delete record, click toolbar Delete button after selecting a row.</li>
        <li><code>Update, Cancel</code> - You can save or discard changes by clicking the toolbar Update and Cancel button respectively.</li>
    </ul>
</SampleDescription>
<ActionDescription>
    <p>The Tree Grid supports CRUD operations. This CRUD operations can be configured in Tree Grid using <code>TreeGridEditSettings</code>. Also, it has different modes to manipulate the datasource.</p>
    <p>The available modes are,</p>
      <ul><li><code>EditMode.Row </code></li>
          <li><code>EditMode.Cell</code></li>
          <li><code>EditMode.Dialog</code></li>
          <li><code>EditMode.Batch</code></li>
       </ul>
     <p>In this demo, Row mode is enabled for editing by default. You can start editing any row by double clicking on it or clicking on toolbar’s Edit button, then the currently selected row will be changed to edited state. You can change the row values and save edited data to the datasource.</p>
     <p> More information about Edit Modes can be found in the <a target='_blank' href='https://blazor.syncfusion.com/documentation/treegrid/edit/#edit-modes' > documentation</a> section.</p>
</ActionDescription>


<div class="col-lg-9 control-section">
    <div class="content-wrapper">
        <div class="row">
               <SfTreeGrid DataSource="TreeData" IdMapping="TaskId" ParentIdMapping="ParentId" Height="315" TreeColumnIndex="1" AllowPaging="true" Toolbar="@ToolbarItems">
                <TreeGridPageSettings PageSize="2"></TreeGridPageSettings>
                <TreeGridEditSettings AllowAdding="true" AllowEditing="true" AllowDeleting="true" Mode="@GridEditingmode" NewRowPosition="RowPosition.Below"></TreeGridEditSettings>
                <TreeGridColumns>
                    <TreeGridColumn Field="TaskId" HeaderText="Task ID" ValidationRules="@(new ValidationRules() { Required = true, Number = true })"  Width="80" IsPrimaryKey="true" TextAlign="Syncfusion.Blazor.Grids.TextAlign.Right"></TreeGridColumn>
                    <TreeGridColumn Field="TaskName" HeaderText="Task Name" ValidationRules="@(new ValidationRules() { Required = true })"  Width="240"></TreeGridColumn>
                    <TreeGridColumn Field="StartDate" HeaderText="Start Date" Format="d" Type=Syncfusion.Blazor.Grids.ColumnType.Date Width="152" TextAlign="Syncfusion.Blazor.Grids.TextAlign.Right" EditType=Syncfusion.Blazor.Grids.EditType.DatePickerEdit></TreeGridColumn>
                    <TreeGridColumn Field="Duration" HeaderText="Duration" ValidationRules="@(new ValidationRules() { Number = true, Min = 0 })" Width="100" TextAlign="Syncfusion.Blazor.Grids.TextAlign.Right"></TreeGridColumn>
                    <TreeGridColumn Field="Progress" HeaderText="Progress" Width="100"></TreeGridColumn>

                </TreeGridColumns>
            </SfTreeGrid>
        </div>
    </div>
</div>

<div class="col-lg-3 property-section">
    <div class="property-panel-header">Properties</div>
    <table id="property" title="Properties" style="width: 100%;">
        <tr style="height: 50px">
            <td class="left-side" style="width: 35% !important; padding: 6px 6px 6px 6px">Edit Mode</td>
            <td>
                <SfDropDownList TValue="string" TItem="DropDownData" @ref="DropDown" DataSource="@DropDownEditmode" @bind-Value="@Editmodes" >
                    <DropDownListEvents TValue="string" TItem="DropDownData" ValueChange="ModeChange"></DropDownListEvents>
                    <DropDownListFieldSettings Text="Name" Value="ID"></DropDownListFieldSettings>
                </SfDropDownList>
            </td>
        </tr>
    </table>
 </div>

        @code{

        SfDropDownList<string, DropDownData> DropDown;

        public List<WrapData> TreeData { get; set; }

        public string Editmodes { get; set; } = "Cell Editing" ;

        public Syncfusion.Blazor.TreeGrid.EditMode GridEditingmode { get; set; }

        public List<string> ToolbarItems { get; set; }

        public List<DropDownData> DropDownEditmode = new List<DropDownData>();


        protected override void OnInitialized()
        {
           

            this.ToolbarItems = new List<string>() { "Add", "Delete", "Update", "Cancel" };
            this.TreeData = WrapData.GetWrapData().ToList();
            this.DropDownEditmode.Add(new DropDownData() {ID="Cell Editing", Name="Cell Editing"});
            this.DropDownEditmode.Add(new DropDownData() {ID="Row Editing", Name="Row Editing"});
        }

        public class DropDownData {
            public string ID { get; set; }
            public string Name { get; set; }
        }

        public void ModeChange(Syncfusion.Blazor.DropDowns.ChangeEventArgs<string, DropDownData> args)

        {
            if(args.Value == "Cell Editing")
            {
                ToolbarItems = new List<string>() { "Add", "Delete", "Update", "Cancel" };
                GridEditingmode = Syncfusion.Blazor.TreeGrid.EditMode.Cell;
            }
            if(args.Value == "Row Editing")
            {
                ToolbarItems = new List<string>() { "Add", "Edit", "Delete", "Update", "Cancel" };
                GridEditingmode = Syncfusion.Blazor.TreeGrid.EditMode.Row;
            }
        }
        public class TreeEditModes
        {
            public string ID { get; set; }
            public string Name { get; set; }
        }
        }
